<?php 

class cardController extends appController
{
    public $wxid = 0;//数据库Id
    
    public $m;//自己的model
    public $info=array();//当前用户的微信信息
    public  function __construct(){
        parent::__construct();
        //基础信息构建
        $this->m = Model('card');
       

//         $_SESSION['wxid'] = 1;
        //当前用户的微信id
        $this->wxid = $_SESSION['wxid']?$_SESSION['wxid']:0;
    
//         if($this->wxid>0)
//         {
//             //当前用户的微信信息
//             $this->info = $this->m->get_record($this->wxid);
//             $this->assign('info',$this->info);
//         }
        
    
    }
	
	public function clear()
	{
		
		unset($_SESSION['wxid']);
		unset($_SESSION);
	}
	
	public function canIn()
	{
	     
	    if(!$this->info)
	    {
	       
	        echo "非法操作";
	        exit;
	    }
	}
	
	
	
	public function chatCard()
	{
	    $card = $this->m->getChatCard();
	    
	    echo json_encode($card);
	}
	
	
	
	public function testTime()
	{
	    $hour1 = strtotime(date('Y-m-d H:i'))-date('i')*60;
	     
	    $minute1 = strtotime(date('Y-m-d H:i'));
	    
	    echo $hour1.'---'.$minute1.'--'.time();
	    
	}
	
	public function ajaxCard()
	{
	    
	   
	    
// 	    var_dump($card);
        if($res['error']==0)
        {
            //$res = array();
    	    //$cardList[]['cardId'] = "pmE3vjmiNy6czGQzN_pHD-EdR7IM";
            $cardList[]['cardId'] = $res['data']['card_id']; 
    	    $jsdk_card = $this->m->build_jsdk_card($cardList);
            
    	    $res['content'] ='卡券投放成功。';
    	    $res['data'] =$jsdk_card;
    	    
    	    
    	    
    	    
    	    $user['wxid'] = $_SESSION['wxid'];
    	    $user['cid'] = $card_id;
    	    $user['change_integral'] = $change_integral;
    	    $user['num'] = 1;
    	    $user['addtime'] = time();
    	    
    	    
    	    
    	    $id = $this->m->insertCardUser($user);
    	    
    	    if($id==-1)
    	    {
    	        $res['error']=1;
    	        $res['content']="你已经领取过该卡券了。";
    	    }
    	    
    	    
    	    $res['id'] =$id;
    	    
    	    
    	    
	    }
// 	    exit;

// 	    var_dump($res);
//         return $res;
        
	    die(json_encode($res));
	   
	}
	

	
	public function record()
	{
	    //微信分享参数构建
	    $jsdk = $this->m->get_jsdk();
	    
	    $this->assign("jsdk", $jsdk);
	  
		$this->check_member();
		
		$records = $this->m->getCardList();
		
		$this->assign("records", $records);
		
		$this->assign_global($config);
	   
		$this->display("record.html");
	}
	
	public function set_session()
	{
	    $_SESSION['wxid'] = 12498;
	    $this->record();
	}
	
	public function record2()
	{
	   
	     
	   //微信分享参数构建
	   $jsdk = $this->m->get_jsdk();
	   
	   $this->assign("jsdk", $jsdk);
	   
	   $records = $this->m->getCardList();
	   
	   $this->assign("records", $records);
	   
	    //$this->check_member2();
	
	    $this->assign_global($config);
	
	    $this->display("record.html");
	}
	
	public function getCard(){
	    
	    
	    $change_integral = 0;
	    
	    if($_REQUEST['type']=="chat")
	    {
	         
	        $card = $this->m->getChatCard();
	         
	        $card_id = $card['id'];
	         
	         
	         
	    }
	    else
	    {
	         
	        $card_id = $_REQUEST['card_id']>0?$_REQUEST['card_id']:"";
	        	
	        	
	        	
	    }
	    

	    $back = $this->m->is_saved_card($card_id,$_SESSION['wxid']);
	    
	    if($back)
	    {
	        $res['error']=3;
	        $res['content']="你已经领取过该卡券了。";
	    }
	    else
	    {
	        
    	    $res = $this->m->getCard($card_id);
    	    
    	    $change_integral = $res['data']['change_integral'];
    	    
    	    $wxRecord = $this->m->getWxRecord($_SESSION['wxid']);
    	     
    	    if($wxRecord['integral']<$change_integral)
    	    {
    	        $res['error']=3;
    	        $res['content']="你的积分不足。";
    	    }
	    }
        
	     
	    if($res['error']!=0)
	    {
	        die(json_encode($res));
	    } 
	    
	    
	    $back = $this->m->is_saved_user($_SESSION['wxid']);
// 	    if($back)
// 	    {
// 	        $this->ajaxCard();
// 	    }
	    if(!$back&&!isset($_REQUEST['name'])&&!isset($_REQUEST['phone']))
        {
            $res['error'] = 2;
            $res['content'] = "首次领取卡券。";
            die(json_encode($res));
        }
        else
	    {
	        if(!$back)
	        {
        	    $user['phone'] =isset($_REQUEST['phone'])?$_REQUEST['phone']:"";
        	    if(preg_match("/1[3458]{1}\d{9}$/",$user['phone'])){
        	
        	    }else{
        	        $res['error'] = 1;
        	        $res['content'] = "请填写正确的11位手机号码。";
        	        die(json_encode($res));
        	    }
        	    
        	    $user['name'] = isset($_REQUEST['name'])?$_REQUEST['name']:"";
        	    
        	    $user['wxid'] = $_SESSION['wxid'];
        	    $user['addtime'] = time();
    	    
    	    
    	        
        	    $id = $this->m->insert($user,"card_user");
    	    }
    	    
    	    
    	    $cardList[]['cardId'] = $res['data']['card_id'];
    	    $jsdk_card = $this->m->build_jsdk_card($cardList);
    	    
    	    $res['content'] ='卡券投放成功。';
    	    $res['data'] =$jsdk_card;
    	    	
    	    	
    	    	
    	    $user = array();
    	    $user['wxid'] = $_SESSION['wxid'];
    	    $user['cid'] = $card_id;
    	    $user['change_integral'] = $change_integral;
    	    $user['num'] = 1;
    	    $user['addtime'] = time();
    	    $user['date'] = date("Y-m-d");
    	    
    	    $id = $this->m->insertCardUser($user);
    	    	
    	    if($id==-1)
    	    {
    	        $res['error']=3;
    	        $res['content']="你已经领取过该卡券了。";
    	    }
    	    	
    	    	
    	    $res['id'] =$id;
    	    
    	    die(json_encode($res));
	    }
	    
	}
	
	
	public function backCard()
	{
	    
	    $res['error']=0;
	    $res['content']="领取成功。";
	    
	    $id = $_REQUEST['id'];
        
	   
	    
	    $wxid = $_SESSION['wxid'];
	    
	    if($id == 0  || $wxid == 0)
	    {
	        $res['error']=1;
	        $res['content']="非法操作。";
	    }
	    
	    $integral = $this->m->saveInfo($id,$wxid);
        
	    $res['integral'] = $integral;
	    
	    die(json_encode($res));
	    
	}
	
	
	
	

}

?>